package com.zkyc.framework.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zkyc.framework.common.dto.PageDto;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.pojo.Activity;
import com.zkyc.framework.common.utils.WebsiteUtil;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.common.mapper.ActivityMapper;
import com.zkyc.framework.website.service.ActivityService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (Activity)表服务实现类
 *
 * @author wj
 * @since 2022-11-09 17:10:16
 */
@Service("activityService")
public class ActivityServiceImpl implements ActivityService {
    @Resource
    private ActivityMapper activityMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Activity queryById(Integer id) {
        return this.activityMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public ResultVO queryByPage(int page ,int size,int  status) {
        PageDto pageDto = new PageDto();
        pageDto.setPage(page);
        pageDto.setSize(size);
        pageDto.checkParam();
        Page pages = new Page(pageDto.getPage(), pageDto.getSize());
        String websiteId = WebsiteUtil.getWebsiteId();
        LambdaQueryWrapper<Activity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (status==0){
            lambdaQueryWrapper.eq(Activity::getStatus,0);
        }
        lambdaQueryWrapper.eq(Activity::getWebsiteId,websiteId);
       lambdaQueryWrapper.orderByDesc(Activity::getUpdateTime);
        IPage<Activity> page1 = activityMapper.selectPage(pages, lambdaQueryWrapper);
        return ResultVO.data(page1);
    }

    /**
     * 新增数据
     *
     * @param activity 实例对象
     * @return 实例对象
     */
    @Override
    public ResultVO insert(Activity activity) {
        if (activity == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        Integer websiteId = Integer.valueOf(WebsiteUtil.getWebsiteId());
        activity.setWebsiteId(websiteId);
        activity.setStatus(0);
        activityMapper.insert(activity);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    /**
     * 修改数据
     *
     * @param activity 实例对象
     * @return 实例对象
     */
    @Override
    public ResultVO update(Activity activity) {
        if (activity == null && activity.getId() == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (queryById(activity.getId()) == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        activity.setUpdateTime(new Date());
        activityMapper.updateById(activity);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public ResultVO deleteById(Integer id) {
        if (id == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (queryById(id) == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        activityMapper.deleteById(id);
        return ResultVO.success(ResultCode.SUCCESS);
    }
}
